<?php
class Book
{
	public $_book;
	
	function __construct()
	{
		$this->_book = new Crb_Db('book');
	} 
	
	
	function getBlockDates($roomType)
	{
		$select = $this->_book->select()->where('room = ?',$roomType);
		$Rows = $this->_book->fetchAll($select);
		if (!$Rows) return array();
		$Result = array();
		$Dates = $Rows->toArray();
		
		foreach ($Dates as $Date)
		{
			$Result[] = $Date['date']; 	
		}
		
		return $Result;
	} 
	
	function setBlockInterval($dateFrom, $dateTo, $roomType)
	{
		$dDateFrom = strtotime($dateFrom);//time();
		$dDateTo   = strtotime($dateTo);//time()+ 60*60*24*10;
		
		if ($dDateFrom > $dDateTo) return false;
		
		$sDateFrom = "'" . date("Y-m-d",$dDateFrom) . "'";
		
		$IntervalDays = ($dDateTo - $dDateFrom)/(60*60*24);
		for ($i = 0; $i <= $IntervalDays; $i++)
		{
			 $this->setBlockDate(new Zend_Db_Expr($sDateFrom . "+ INTERVAL " . $i . " DAY"), $roomType);
		}
	}
	
	
	function checkInterval($dateFrom, $dateTo, $roomType)
	{
	    $dDateFrom = strtotime($dateFrom);//time();
		$dDateTo   = strtotime($dateTo);//time()+ 60*60*24*10;
		
		if ($dDateFrom > $dDateTo) return false;
		
		$sDateFrom = "'" . date("Y-m-d",$dDateFrom) . "'";
		
		$IntervalDays = ($dDateTo - $dDateFrom)/(60*60*24);
		
		
		for ($i = 0; $i <= $IntervalDays; $i++)
		{
			
		     if( $this->checkBlockDate(new Zend_Db_Expr($sDateFrom . "+ INTERVAL " . $i . " DAY"), $roomType)   )  
		     {
		       return false;  
		     } 
		    
		}
	    
	    return true;
	}
	
	
	function setBlockDate($date, $roomType)
	{
		$Row = $this->_book->createRow(array('date'=>$date, 'room'=>$roomType));
		$Row->save();
	}
	
	
	function checkBlockDate($date, $roomType)
	{
		$Row = $this->_book->fetchRow($this->_book->select()->where("date = ?", $date)->where("room = ?", $roomType) );
		if ($Row) return true; else return false; 
	}
	
	
	
}